<?php


class LogController extends ControllerBase
{

    public function listAction()
    {
        $pageNum = $this->request->getQuery("pageNum", null, 1);
        $size = 50;
        $limit = ($pageNum - 1) * 50;
        $page_name = $this->request->get("page_name");
        $img_name = $this->request->get("img_name");
        $wxh_id = $this->request->get("wxh_id");
        $where = " 4=4 ";
        if ($page_name) {
            $where .= " and page_name like '{%$page_name%}'";
        }
        if ($img_name) {
            $where .= " and img_name like '{%$img_name%}'";
        }
        if ($wxh_id) {
            $where .= " and wxh_id like '{%$wxh_id%}'";
        }

        $list = PageLog::find([
            "conditions" => $where,
            "limit" => $size,
            "order" => "create_at desc ",
            "offset" => $limit
        ]);
        $total = PageLog::count($where);
        $allPageNum = ceil($total / $size);

        $this->exitMessage(true, '', ["data" => $list, "pageInfo" => ["size" => $size, "current" => $pageNum, "allPageNum" => $allPageNum, "total" => $total]]);
    }

    public function countAction(){
         $page_id = $this->request->get("page_id");
         $img_id = $this->request->get("img_id");
         $type = $this->request->get("type");
         if(!$page_id||!$img_id||!$type){
             $this->exitMessage(false,"缺少必要参数");
         }
         $page_info = PageInfo::findFirst($page_id);
         $img_info = ImgInfo::findFirst($img_id);
         $log = PageLog::findFirst("page_id={$page_id} and img_id={$img_id}");
         if(!$log){
             $log = new PageLog();
             if ($type=="access"){
                 $log->access_num =1;
             }
             if($type=="handle"){
                 $log->handle_num=1;
             }
             $log->create_at = date("Y-m-d H:i:s");
         }else{
             if ($type=="access"){
                 $log->access_num +=1;
             }
             if($type=="handle"){
                 $log->handle_num +=1;
             }
             $log->update_at = date("Y-m-d H:i:s");
         }
         $log ->page_name = $page_info->name;
         $log ->page_id = $page_id;
         $log ->img_name = $img_info->name;
         $log ->img_id = $img_id;
         $log ->wxh_id = $img_info->wxh_id;
         $res = $log->save();
         if(!$res){
             $this->exitMessage(false,"保存失败");
         }
         $this->exitMessage(true,"保存成功");
     }
}